using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Web.Core;
using Web.Core.Objects;

public partial class AdminCP_Categories : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {        
        if (Session["Language"] == null)
            Session["Language"] = Web.Core.GlobalStorage.Language.Vietnamese;
        int page = 1; 
        int pageSize = GlobalStorage.categoriesPerPageForAdmin;
        if (Request.Params["page"] != null)
            page = int.Parse(Request.Params["page"]);
        //ArrayList arrCategories = Categories.GetAllCategories((int)Session["Language"],page,pageSize);
        ArrayList arrCategories = Categories.GetParentCategories((int)Session["Language"]);
        arrCategories = AdjustmentDescription(arrCategories, 20);
        if (!IsPostBack)
        {
            this.rptCategories.DataSource = arrCategories;
            this.rptCategories.DataBind();
        }
        // Pager
        this.Pager1.objectsPerPage = GlobalStorage.categoriesPerPageForAdmin;
        this.Pager1.displayAll = false; // Chi hien thi ben phai
        this.Pager1.totalObjects = Categories.CountAllCategories((int)Session["Language"]);
        // ---------------
        if (Request.Params["do"] != null)
        {
            string cDo = Request.Params["do"];
            switch (cDo)
            {
                case "edit":
                    if (Request.Params["cid"] != null)
                        LoadControls();
                    break;
                case "add":
                    LoadControls();
                    break;
                case "del":
                    if (Request.Params["cid"] != null)
                    {
                        int catID = 0;
                        try
                        {
                            catID = int.Parse(Request.Params["cid"].ToString());
                        }
                        catch { }
                        Categories.DeleteCategory(new Category(catID,"",0,0,0));
                        this.PreRenderComplete += new EventHandler(AdminCP_Categories_PreRenderComplete);                        
                    }
                    break;
                default:
                    break;
            }
        }
    }

    void AdminCP_Categories_PreRenderComplete(object sender, EventArgs e)
    {
        Response.Redirect("Categories.aspx");
    }
    ArrayList AdjustmentDescription(ArrayList arr, int len)
    {
        foreach (Category cat in arr)
        {
            if (cat.Description.Length > len)
                cat.Description = cat.Description.Substring(0, len) + " ...";
        }
        return arr;
    }

    void LoadControls()
    {
        Control addEditCategories = (Control)Page.LoadControl("~/AdminCP/AdminControls/AddEditCategory.ascx");
        this.PlaceHolderBottom.Controls.Add(addEditCategories);
    }
    public string getAllChild(int catID)
    {
        string str = "";
        ArrayList list = Categories.GetChild(catID, (int)Session["Language"]);
        foreach (Category item in list)
        {
            str += "<tr><td class='first'>" + item.Id + "</td>";
            str += "<td class='first style2'>" + item.Name + "</td>";
            str +="<td>"+item.Description+"</td>";
            str +="<td>"+Manufacturers.GetManufacturersByID(item.Manufact).MName+"</td>";
            str +="<td>"+item.Image+"</td>";
            str += string.Format("<td><a href='Categories.aspx?do=edit&cid={0}'><img src='AdminImages/edit-icon.gif' /></a>&nbsp;<a href='Categories.aspx?do=del&cid={0}'><img src='AdminImages/hr.gif' /></a></td></tr>", item.Id);
        }

        return str;
    }
}
